import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;

public class Day16 {

}

// 用队列实现栈
class MyStack {

    Queue<Integer> queue1;
    Queue<Integer> queue2;

    public MyStack() {
        queue1 = new LinkedList<>();
        queue2 = new LinkedList<>();
    }

    private void move(Queue<Integer> dest, Queue<Integer> src, int len) {
        for(int i = 0; i < len && !src.isEmpty(); i++) {
            dest.offer(src.poll());
        }
    }

    public void push(int x) {
        if(!queue1.isEmpty()) {
            queue1.offer(x);
        } else {
            queue2.offer(x);
        }
    }
    //pop: type = 0 top: type = 1
    private int popOrtop(int type) {
        if(empty()) return -1;
        int ret;
        Queue<Integer> dest = null;
        if(!queue1.isEmpty()) {
            move(queue2, queue1, queue1.size() - 1);
            ret = queue1.poll();
            dest = queue2;
        } else {
            move(queue1, queue2, queue2.size() - 1);
            ret = queue2.poll();
            dest = queue1;
        }
        if(type == 1) dest.offer(ret);
        return ret;
    }
    public int pop() {
        return popOrtop(0);
    }

    public int top() {
        return popOrtop(1);
    }

    public boolean empty() {
        return queue1.isEmpty() && queue2.isEmpty();
    }
}

// 用栈实现队列
class MyQueue {
    Stack<Integer> stack1, stack2;
    public MyQueue() {
        stack1 = new Stack<>();
        stack2 = new Stack<>();
    }

    public void push(int x) {
        stack1.push(x);
    }

    private void move(Stack<Integer> dest, Stack<Integer> src, int len) {
        for(int i = 0; i < len && !src.isEmpty(); i++) {
            dest.push(src.pop());
        }
    }

    public int pop() {
        if(empty()) return -1;
        if(stack2.isEmpty()) move(stack2, stack1, stack1.size());
        return stack2.pop();
    }

    public int peek() {
        if(empty()) return -1;
        if(stack2.isEmpty()) move(stack2, stack1, stack1.size());
        return stack2.peek();
    }

    public boolean empty() {
        return stack2.isEmpty() && stack1.isEmpty();
    }
}